#pragma once
#include <QObject>


class QTcpSocket;
class QString;
class QObject;
class LoginAction;
class ActionBase;
class GetUserListAction;
class UserInfo;
class UserMessageAction;


class QMsgManager : public QObject
{
    Q_OBJECT
public:
    QMsgManager();
    ~QMsgManager();

    static QMsgManager& GetInstance();

    void QueryUserName(QTcpSocket* pSock, QString const& userName);

    void ProcessAction(QTcpSocket* pSock, QString const& strAction);

    void RefreshUserList(QTcpSocket* pSock, QString const& userName, QString const& token);

    static void WriteSocketData(QTcpSocket* pSocket, QString const& str);

    void DoSendMessage(QTcpSocket* m_pTcpSock, QString const& strUserName, QString const& clientToken, QString const& userMsg);

    virtual void OnProcessAction(QTcpSocket* pSock, QString const& actionString);

signals:
    // server
    void sigQueryUserName(QTcpSocket* pSock, QString const& userName);

    void sigLogin(QTcpSocket* pSock, QString const& userName);

    void sigGetUserInfoList(QTcpSocket* pSock, QString const& userName, QString const& token);

    void sigSendUserMessage(QTcpSocket* pSock, QString const& token, QString const& userName, QString const& msg);

    // client
    void sigQueryUserNameResult(bool isOk, QString const& errMsg);

    void sigLoginResult(bool isOk, QString const& userName, QString const& token, QString const& errMsg);

    void sigGetUserInfoListResult(QString const& userName, QString const& token, QVector<UserInfo> const& userInfoList);

    void sigGetUserMessage(QString const& userName, QString const& msg);

public slots:
    void OnGetSockReadReady();

protected:
    void ProcessLoginAction(std::shared_ptr<LoginAction> pAction);
    void ProcessQueryUserName(std::shared_ptr<LoginAction> pAction);
    void ProcessQueryUserNameResult(std::shared_ptr<LoginAction> pAction);
    void ProcessLoginResult(std::shared_ptr<LoginAction> pAction);
    void ProcessGetUserInfoListResult(std::shared_ptr<GetUserListAction> pAction);
    void ProcessGetUserMessage(std::shared_ptr<UserMessageAction> pAction);
};

